# frozen_string_literal: true

{{#generated_src_warning}}
{{generated_src_warning}}
{{/generated_src_warning}}

require_relative 'spec_helper'

module {{module_name}}
  describe EndpointProvider do
    subject { {{module_name}}::EndpointProvider.new }

    {{#endpoint_tests}}
    context "{{{documentation}}}" do
      let(:expected) do
        {{{expect}}}
      end

      it 'produces the expected output from the EndpointProvider' do
        params = EndpointParameters.new(**{{{params}}})
        {{#expect_error?}}
        expect do
          subject.resolve_endpoint(params)
        end.to raise_error(ArgumentError, expected['error'])
        {{/expect_error?}}
        {{^expect_error?}}
        endpoint = subject.resolve_endpoint(params)
        expect(endpoint.url).to eq(expected['endpoint']['url'])
        expect(endpoint.headers).to eq(expected['endpoint']['headers'] || {})
        expect(endpoint.properties).to eq(expected['endpoint']['properties'] || {})
        {{/expect_error?}}
      end
      {{#operation_inputs}}

      it 'produces the correct output from the client when calling {{operation_name}}' do
        client = Client.new(
          {{#client_params}}
          {{param}}: {{{value}}},
          {{/client_params}}
          stub_responses: true
        )
        {{#expect_error?}}
        expect do
          client.{{operation_name}}(
            {{#operation_params}}
            {{param}}: {{{value}}},
            {{/operation_params}}
          )
        end.to raise_error(ArgumentError, expected['error'])
        {{/expect_error?}}
        {{^expect_error?}}
        {{#expect_auth?}}
        {{#s3_express_auth?}}
        client.stub_responses(:create_session, credentials: {
          access_key_id: 's3-akid',
          secret_access_key: 's3-secret',
          session_token: 's3-session',
          expiration: Time.now + 60 * 5
        })
        expect_auth({"name"=>"sigv4", "signingName"=>"s3express"})
        Aws::S3.express_credentials_cache.clear
        {{/s3_express_auth?}}
        expect_auth({{{expected_auth}}})
        {{/expect_auth?}}
        resp = client.{{operation_name}}(
          {{#operation_params}}
          {{param}}: {{{value}}},
          {{/operation_params}}
        )
        expected_uri = URI.parse(expected['endpoint']['url'])
        expect(resp.context.http_request.endpoint.to_s).to include(expected_uri.host)
        expect(resp.context.http_request.endpoint.to_s).to include(expected_uri.scheme)
        expect(resp.context.http_request.endpoint.to_s).to include(expected_uri.path)
        {{#expect_headers?}}
        {{#expected_headers}}
        expect(resp.context.http_request.headers['{{param}}']).to eq({{{value}}})
        {{/expected_headers}}
        {{/expect_headers?}}
        {{/expect_error?}}
      end
      {{/operation_inputs}}
    end

    {{/endpoint_tests}}
  end
end
